package com.vf.cloud.paas.app.master.api.asset.controller;

import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.asset.service.IAssetClassify;
import com.vf.cloud.paas.app.master.common.domain.AssetClassify;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/asset/classify")
public class AssetClassifyController {

	@Autowired
	private IAssetClassify assetClassifyImpl;

	@RequestMapping(value = "/lazy", method = RequestMethod.POST)
	public R<List<AssetClassify>> lazy(@RequestBody AssetClassify param) {
		return R.ok(assetClassifyImpl.getChildrens(param));
	}
	
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<AssetClassify>> list(@RequestBody AssetClassify param) {
		List<AssetClassify> rootList=new LinkedList<AssetClassify>();
		AssetClassify root=new AssetClassify();
		root.setId("-1");
		root.setPid("root");
		root.setName("默认");
		root.setSort(0);
		root.setChildren(assetClassifyImpl.getChildren(param));
		rootList.add(root);
		return R.ok(rootList);
	}
	

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<AssetClassify> save(@RequestBody AssetClassify assetClassify) {
		if (assetClassify == null) {
			return R.failed("参数必填.");
		}

		if (StrKit.isBlank(assetClassify.getId())) {
			if (StrKit.isBlank(assetClassify.getPid()))
				assetClassify.setPid("-1");
			assetClassify.setId(UuidUtil.getUUID());
			if (assetClassify.save())
				return R.ok(assetClassify);
		} else {
			if (assetClassify.update())
				return R.ok(assetClassify);
		}
		return R.failed();
	}

	@Tx
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody AssetClassify assetClassify) {
		try {
			if (assetClassify == null || StrKit.isBlank(assetClassify.getId())) {
				return R.failed("参数必填.");
			}
			assetClassifyImpl.delete(assetClassify.getId());
			return R.ok();
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}
